    //
//  RS3ViewController.m
//  SmartRoadSh//
//  Created by simon on 10-9-17.
//  Copyright 2010 simonwang142232@gmail.com. All rights reserved.
//
//	http://api.escdn.com/roadshow_version/data.xml?99
//

#import "RS3ViewController.h"
#import "SendHttpRequest.h"
#import "MyDataSource.h"
#import "ArtTour.h"
#import "MyPhotoView.h"
#import <QuartzCore/QuartzCore.h>
#import "MyTableViewCell.h"
#import "DetailWebViewController.h"
#import "DateUtil.h"
#import "MyViewController.h"
#import "SmartRoadShowAppDelegate.h"
#import "MyTabBarController.h"
#import "MyNavigationController.h"
#import "CacheManager.h"
#import <CommonCrypto/CommonDigest.h>
#import "Opinion.h"
#import "Reachability.h"
#import "OpenWaitView.h"
#import "NSData+Base64.h"
#import "CallUtil.h"
#import "CacheManager.h"
#import "extev.h"

@interface NSString (MyExtensions)
- (NSString *) md5;
@end

@implementation NSString (MyExtensions)
- (NSString *) md5
{
    const char *cStr = [self UTF8String];
    unsigned char result[16];
    CC_MD5( cStr, strlen(cStr), result ); // This is the md5 call
    return [NSString stringWithFormat:
			@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
			result[0], result[1], result[2], result[3], 
			result[4], result[5], result[6], result[7],
			result[8], result[9], result[10], result[11],
			result[12], result[13], result[14], result[15]
			];  
}

@end





@implementation RS3ViewController
@synthesize segmentControl,scrollView,stationArray,shareImage;
@synthesize submitCityString;


#pragma mark MySegmentedControl Delegate


- (void)segmentedControl:(TESegmentedControl *)segmentedControl segmentDidChangeToIndex:(NSInteger)index{
    
	for (UIView *subview in self.view.subviews){
		if (subview.tag == 789 || subview.tag == 311) {
			[subview removeFromSuperview];
		}
	}
	
	switch (index) {
		case 0:
			for (UIView *subview in self.view.subviews){
				if (subview.tag == 301 || subview.tag == 302) {
					[subview removeFromSuperview];
				}
			}
			[self.view addSubview:firstView];
			break;
		case 1:
			for (UIView *subview in self.view.subviews){
				if (subview.tag == 300 || subview.tag == 302) {
					[subview removeFromSuperview];
				}
			}
			[self.view addSubview:secondView];
			
			break;
		case 2:
			for (UIView *subview in self.view.subviews){
				if (subview.tag == 300 || subview.tag == 301) {
					[subview removeFromSuperview];
				}
			}
            
			[self.view addSubview:thirdWebView];
			
			break;
			
		default:
			break;
	}
}

//没有网络时，当要使用到网络时提出警告

- (BOOL)showAlertWhileNoConnection{
	Reachability *internetReach = [Reachability reachabilityForInternetConnection];
	[internetReach startNotifier];	
	NetworkStatus netStatus = [internetReach currentReachabilityStatus];
	
	if (netStatus) {
		return YES;
	}
	else {
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"本功能需要网络支持，请检查网络连接！"
													   delegate:nil cancelButtonTitle:@"确定"
											  otherButtonTitles:nil];
		[alert show];
		[alert release];		
		return NO;
	}
}


//刷新都市巡演信息

- (void)reloadAllData{
	Reachability *internetReach = [Reachability reachabilityForInternetConnection];
	[internetReach startNotifier];	
	NetworkStatus netStatus = [internetReach currentReachabilityStatus];
	if (netStatus) {
		reload = YES;
		
		NSString *versionRequest = kRoadShowVersion;
		NSString *version = [SendHttpRequest sendHttpPostRequestWithURLString:versionRequest postString:nil
															   decodeEncoding:NSUTF8StringEncoding];
		
		if (![[[NSUserDefaults standardUserDefaults] objectForKey:@"version"] isEqualToString:version]) {
			
			[[NSUserDefaults standardUserDefaults] setObject:version forKey:@"version"];
			
			NSString *urlstring = kRoadshowCity;
			NSString *documentStr = [SendHttpRequest sendHttpPostRequestWithURLString:urlstring postString:nil decodeEncoding:NSUTF8StringEncoding];
					
			stationArray = [MyDataSource getStationInfo:documentStr];
			
			NSLog(@"station count = %d",[stationArray count]);
			
			[scrollView setContentSize:CGSizeMake(270 * [stationArray count] + 60, 290)];
			
			if (stationArray != nil && [stationArray count] != 0) {
				[CacheManager setXML:documentStr withFileName:@"Station"];
				
				
				for (UIView *subview in scrollView.subviews){
					[subview removeFromSuperview];
				}
				
				NSCalendar* calendar = [[[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar] autorelease];
				
				NSDateComponents* comps = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit |
										   NSWeekCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | 
										   NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:[NSDate date]];
				
				NSString *year = [NSString stringWithFormat:@"%d",comps.year];
				NSString *month = nil;
				NSString *day = nil;
				
				
				if (comps.month < 10) {
					month = [NSString stringWithFormat:@"0%d",comps.month];
				}
				else {
					month = [NSString stringWithFormat:@"%d",comps.month];
				}
				
				if (comps.day < 10) {
					day = [NSString stringWithFormat:@"0%d",comps.day];
				}
				else {
					day = [NSString stringWithFormat:@"%d",comps.day];
				}
				
				NSString *currentTime = [NSString stringWithFormat:@"%@-%@-%@",year,month,day];
				
				
				
				NSInteger leftCity = 0;
                
				for (int i = 0; i<[stationArray count]; i++) {
					NSString *stationTime = [[stationArray objectAtIndex:i] _endtime];
					
					BOOL result = [stationTime compare:currentTime] == NSOrderedDescending;
					
					if (result) {
						leftCity ++;
					}
				}	
				
				
				for (int i = 0;i < [stationArray count]; i++){
					MyPhotoView *photoView = [[MyPhotoView alloc] initWithFrame:CGRectMake(34.5 + 270*i, 10, 251, 265)
																		   From:3
																	   Stations:stationArray
																 CurrentStation:i];
					photoView.leftCityCount = leftCity;
					
					photoView.rs3VC = self;
					[photoView setPushID:[NSString stringWithFormat:@"%d",[[stationArray objectAtIndex:i] _id]] 
                                    City:[[stationArray objectAtIndex:i] _city]
									Time:[NSString stringWithFormat:@"%@,%@",[[stationArray objectAtIndex:i] _starttime],[[stationArray objectAtIndex:i] _endtime]]];
					
					[photoView setImage:[[stationArray objectAtIndex:i] _thumb]];

					[scrollView addSubview:photoView];
					[photoView release];
					
				}
			}
			
		}
	}
	
}


//点击到该界面时 调用。
- (void)viewWillShow{
	if (!reload) {
		reload = YES;
		
		OpenWaitView *wait = [[OpenWaitView alloc] initWithView:self.view];
		[wait setMode:MBProgressHUDModeIndeterminate];
		[wait setLabelText:@"正在刷新..."];
		[self.view addSubview:wait];
		[wait showWhileExecuting:@selector(reloadAllData) onTarget:self withObject:nil animated:YES];
		[wait release];
		
	}
	for (UIView *subview in self.view.subviews){
		if (subview.tag == 311) {
			[subview removeFromSuperview];
		}
	}
}


- (void)viewDidLoad {
    [super viewDidLoad];
	
	reload = NO;
		
	self.view.backgroundColor = [UIColor colorWithRed:0.79 green:0.78 blue:0.78 alpha:1.0];
		
	segmentControl = [[TESegmentedControl alloc] initWithFrame:CGRectMake(10, 5, 300, 30)];
	[segmentControl setDelegate:self];
	[segmentControl setItems:[NSArray arrayWithObjects:@"巡展信息",@"城市宣言",@"车型展示",nil]];
	[segmentControl setSelectedSegmentIndex:0];
	[self.view addSubview:segmentControl];
	
	/////////////////////////////////////////////////////////////////////////////////////////
	/////////////////////////////巡展信息／／／／／／／／／／////////////////////////////////////
	/////////////////////////////////////////////////////////////////////////////////////////
	
	firstView = [[UIView alloc] initWithFrame:CGRectMake(0, 38, 320, 308)];
	[firstView setBackgroundColor:[UIColor clearColor]];
	firstView.layer.contents = (id)[UIImage imageNamed:@"eventBG.png"].CGImage;
	[firstView setTag:300];
	[self.view addSubview:firstView];
	
	NSString *documentStr = [CacheManager getXMLStringWithFileName:@"Station"];
	stationArray = [MyDataSource getStationInfo:documentStr];
	
	scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 10, 320, 290)];
	[scrollView setShowsHorizontalScrollIndicator:NO];
	[scrollView setDelegate:self];
	[scrollView setContentSize:CGSizeMake(270 * [stationArray count] + 60, 290)];
	[firstView addSubview:scrollView];
	[scrollView release];

	
	NSCalendar* calendar = [[[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar] autorelease];
	
	NSDateComponents* comps = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit |
							   NSWeekCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | 
							   NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:[NSDate date]];
		
	NSString *year = [NSString stringWithFormat:@"%d",comps.year];
	NSString *month = nil;
	NSString *day = nil;
	
	
	if (comps.month < 10) {
		month = [NSString stringWithFormat:@"0%d",comps.month];
	}
	else {
		month = [NSString stringWithFormat:@"%d",comps.month];
	}
	
	if (comps.day < 10) {
		day = [NSString stringWithFormat:@"0%d",comps.day];
	}
	else {
		day = [NSString stringWithFormat:@"%d",comps.day];
	}
	
	NSString *currentTime = [NSString stringWithFormat:@"%@-%@-%@",year,month,day];
	
	
	NSInteger leftCity = 0;
	for (int i = 0; i<[stationArray count]; i++) {
		NSString *stationTime = [[stationArray objectAtIndex:i] _endtime];
						
		BOOL result = [stationTime compare:currentTime] == NSOrderedDescending;
				
		if (result) {
			leftCity ++;
		}
	}	
	
	for (int i = 0;i < [stationArray count]; i++){
		MyPhotoView *photoView = [[MyPhotoView alloc] initWithFrame:CGRectMake(34.5 + 270*i, 10, 251, 265)
															   From:3
														   Stations:stationArray
													 CurrentStation:i];
		
		photoView.rs3VC = self;
		[photoView setPushID:[NSString stringWithFormat:@"%d",[[stationArray objectAtIndex:i] _id]] City:[[stationArray objectAtIndex:i] _city]
						Time:[NSString stringWithFormat:@"%@,%@",[[stationArray objectAtIndex:i] _starttime],[[stationArray objectAtIndex:i] _endtime]]];
		[photoView setImage:[[stationArray objectAtIndex:i] _thumb]];

		photoView.leftCityCount = leftCity;
		
		[scrollView addSubview:photoView];
		[photoView release];
		
	}
	
	//每次启动时定位到离当前日期最近的活动信息 ---->这个功能没有实现
	NSInteger currentIndex = 0;

	NSString *currentDate = [NSString stringWithFormat:@"%@-%@-%@",year,month,day];
	for (int i = 0; i < [stationArray count]; i++) {
		
		NSString *start = [[stationArray objectAtIndex:i] _starttime];
		NSString *end = [[stationArray objectAtIndex:i] _endtime];
		if ([DateUtil diffBetweenDateStr:currentDate andDateStr:start] && [DateUtil diffBetweenDateStr:end andDateStr:currentDate]) {
			currentIndex = i;
			break;
		}
		else if ([DateUtil diffBetweenDateStr:start andDateStr:currentDate]){
			currentIndex = i;
			break;
		}
		
	}
	[scrollView setContentOffset:CGPointMake(currentIndex * 270, 0)];
	
	/////////////////////////////////////////////////////////////////////////////////////////
	///////////////////////////////////////城市宣言///////////////////////////////////////////
	/////////////////////////////////////////////////////////////////////////////////////////

	secondView = [[UIView alloc] initWithFrame:CGRectMake(0, 38, 320, 308)];
	[secondView setBackgroundColor:[UIColor clearColor]];
	secondView.layer.contents = (id)[UIImage imageNamed:@"dingge.png"].CGImage;
	secondView.layer.cornerRadius = 8;
	secondView.layer.masksToBounds = YES;
	[secondView setTag:301];
	
	UIButton *introduce = [UIButton buttonWithType:UIButtonTypeCustom];
	[introduce setBackgroundColor:[UIColor clearColor]];
	[introduce setImage:[UIImage imageNamed:@"introduce.png"] forState:UIControlStateNormal];
	[introduce addTarget:self action:@selector(seeEventIntroduction) forControlEvents:UIControlEventTouchUpInside];
	introduce.frame = CGRectMake(240, 135, 60, 40);
	[secondView addSubview:introduce];
	
	UIButton *shoot = [UIButton buttonWithType:UIButtonTypeCustom];
	[shoot addTarget:self action:@selector(shootPictureAndUpload) forControlEvents:UIControlEventTouchUpInside];
	shoot.frame = CGRectMake(25, 200, 272, 41);
	[shoot setBackgroundImage:[UIImage imageNamed:@"shoot.png"] forState:UIControlStateNormal];
	[shoot setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
	[secondView addSubview:shoot];
	
	UIButton *visit = [UIButton buttonWithType:UIButtonTypeCustom];
	[visit addTarget:self action:@selector(visitMoreOpinions) forControlEvents:UIControlEventTouchUpInside];
	visit.frame = CGRectMake(25, 250, 272, 41);
	[visit setBackgroundImage:[UIImage imageNamed:@"visit.png"] forState:UIControlStateNormal];
	[visit setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
	[secondView addSubview:visit];
	
	shoot.layer.cornerRadius = 3;
	shoot.layer.masksToBounds = YES;
	
	visit.layer.cornerRadius = 3;
	visit.layer.masksToBounds = YES;
	
	
	/////////////////////////////////////////////////////////////////////////////////////////
	///////////////////////////////////////车型展示///////////////////////////////////////
	/////////////////////////////////////////////////////////////////////////////////////////
	
	thirdWebView = [[UIWebView alloc] initWithFrame:CGRectMake(1, 40, 320, 303)];
	thirdWebView.layer.borderWidth = 5;
	[thirdWebView setBackgroundColor:[UIColor whiteColor]];
	thirdWebView.delegate = self;
	thirdWebView.layer.borderColor = [[UIColor colorWithRed:0.79 green:0.78 blue:0.78 alpha:1.0] CGColor];
	thirdWebView.layer.cornerRadius = 10;
	thirdWebView.layer.masksToBounds = YES;
	[thirdWebView setTag:302];
	
	NSString *path = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/CarShow/show.html"];
	[thirdWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
	
}


- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request 
 navigationType:(UIWebViewNavigationType)navigationType{
    
	NSString *requestURLString = [NSString stringWithFormat:@"%@",[request URL]];
	
	NSLog(@"%s,%d,request : %@", __FUNCTION__, __LINE__, requestURLString);
	
	if([requestURLString hasSuffix:@"4008110000"]){
		CallUtil *call = [CallUtil getInstance];
		[call callPhoneNumber:@"400-811-0000"];
	}
	return YES;
	
}
//查看”活动介绍“

- (void)seeEventIntroduction{
	
	self.navigationController.title = @"城市宣言";
	for (UIView *subview in self.navigationController.navigationBar.subviews){
		if (subview.tag == 803) {
			[subview setHidden:YES];
		}
	}
	DetailWebViewController *introducionVC = [[DetailWebViewController alloc] init];
	introducionVC.view.backgroundColor = [UIColor lightGrayColor];
	
	NSString *path = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/introduce/introduce.html"];
	[[introducionVC webView] loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
	
	[self.navigationController pushViewController:introducionVC animated:YES];
	[introducionVC release];
}


//浏览更多都市新主张
- (void)visitMoreOpinions{
	
	if ([self showAlertWhileNoConnection]) {
		self.navigationController.title = @"城市宣言";
		for (UIView *subview in self.navigationController.navigationBar.subviews){
			if (subview.tag == 803) {
				[subview setHidden:YES];
			}
		}
		
		currentScrollPage = 0;
		
		NSString *urlstring = kCityShoot;
		NSString *documentStr = [SendHttpRequest sendHttpPostRequestWithURLString:urlstring postString:nil decodeEncoding:NSUTF8StringEncoding];
		
		[opinionsArray release];
		opinionsArray = [[MyDataSource getMoreOpinions:documentStr] retain];
		
		if (opinionsArray != nil && [opinionsArray count] != 0) {
			[CacheManager setXML:documentStr withFileName:@"Opinions"];
		}else {
			documentStr =[CacheManager getXMLStringWithFileName:@"Opinions"];
			opinionsArray = [[MyDataSource getMoreOpinions:documentStr] retain];
		}
		
		if (opinionsArray == nil || [opinionsArray count] == 0) {
			UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"服务器正忙，请稍后再试！"
														   delegate:nil 
												  cancelButtonTitle:@"确定" otherButtonTitles:nil];
			[alert show];
			[alert release];
		}
		else {
			MyViewController *visitViewController = [[MyViewController alloc] init];
			visitViewController.view.backgroundColor = [UIColor lightGrayColor];
			
			opinionsTableView = [[UITableView alloc] initWithFrame:CGRectMake(5, 5, 310, 335) style:UITableViewStylePlain];	
			opinionsTableView.layer.contents = (id)[[UIImage imageNamed:@"opBG.png"] CGImage];
			[opinionsTableView setBackgroundColor:[UIColor clearColor]];
			[opinionsTableView setSeparatorColor:[UIColor clearColor]];
			[opinionsTableView setDelegate:self];
			[opinionsTableView setDataSource:self];
			opinionsTableView.layer.cornerRadius = 8;
			opinionsTableView.layer.masksToBounds = YES;
			[visitViewController.view addSubview:opinionsTableView];
			[opinionsTableView release];
			
			opinionsTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(5, 10, 310, 40)];
			
			UIButton *moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
			[moreButton addTarget:self action:@selector(moreOpinions) forControlEvents:UIControlEventTouchUpInside];
			[moreButton setTitle:@"浏览更多..." forState:UIControlStateNormal];
			[moreButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
			[moreButton setFrame:CGRectMake(105, 5, 100, 30)];
			[opinionsTableView.tableFooterView addSubview:moreButton];
			
			
			[self.navigationController pushViewController:visitViewController animated:YES];
			[visitViewController release];
			
		}
	}
}


//－－－－－－－－－－－－－－城市宣言－－浏览更多都市新主张－－翻页-----------------------------
- (void)moreOpinions{
	
	opinionsWaitView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(80, 10, 20, 20)];
	[opinionsWaitView setHidesWhenStopped:YES];
	[opinionsWaitView startAnimating];
	[opinionsWaitView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
	[opinionsTableView.tableFooterView addSubview:opinionsWaitView];
	[opinionsWaitView release];
	[self performSelector:@selector(getMoreOpinionsExe) withObject:nil afterDelay:0.1];
	
}

- (void)getMoreOpinionsExe{
	
	[opinionsWaitView stopAnimating];
	
	currentOpinionPage ++;
	
	NSString *urlstring = kCityShoot;
	NSString *postStr = [NSString stringWithFormat:@"page=%d",currentOpinionPage];
	NSString *documentStr = [SendHttpRequest sendHttpPostRequestWithURLString:urlstring postString:postStr decodeEncoding:NSUTF8StringEncoding];
	
	NSMutableArray *tempArr = [[MyDataSource getMoreOpinions:documentStr] retain];
	[opinionsArray addObjectsFromArray:tempArr];
	[opinionsTableView reloadData];
	
	[tempArr release];
	
}

#pragma mark------------------------------------


//拍摄我的都市新主张

- (void)shootPictureAndUpload{
	//先设定sourceType为相机，然后判断相机是否可用（ipod）没相机，不可用将sourceType设定为相片库
	UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if (![UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
		sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
	}   
	UIImagePickerControllerQualityType qualityType = UIImagePickerControllerQualityType640x480;
	
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
	picker.videoQuality = qualityType;
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = sourceType;
	[picker setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    [self.navigationController presentModalViewController:picker animated:YES];
    [picker release]; 

}


//拍照完成，进入提交界面
- (void)didFinishShoot:(UIImage *)image{

	self.shareImage = image;
	for (UIView *subview in self.view.subviews){
		if (subview.tag == 301) {
			[subview removeFromSuperview];
		}
	}
	
	sendView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 40, 320, 305)];
	[sendView setContentSize:CGSizeMake(320, 480)];
	[sendView setBackgroundColor:[UIColor clearColor]];
	sendView.layer.contents = (id)[UIImage imageNamed:@"shootBG.png"].CGImage;
	sendView.layer.cornerRadius = 8;
	sendView.layer.masksToBounds = YES;
	[self.view addSubview:sendView];
	[sendView setTag:301];
	[sendView release];
	
    //输入后退出键盘的方法
	UIControl *control = [[UIControl alloc] initWithFrame:CGRectMake(0, 0, 320, 305)];
	[control addTarget:self action:@selector(keyboardBack) forControlEvents:UIControlEventTouchUpInside];
	[sendView addSubview:control];
	[control release];
	
	UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 290, 136)];
	[imageView setContentMode:UIViewContentModeScaleAspectFit];
	[imageView setImage:image];
	[sendView addSubview:imageView];
	[imageView release];
	
	userID = [[UITextField alloc] initWithFrame:CGRectMake(20, 180, 210, 30)];
	[userID setDelegate:self];
	userID.tag = 480;
	[userID setKeyboardType:UIKeyboardTypeEmailAddress];
	[userID setBorderStyle:UITextBorderStyleRoundedRect];
	[userID setPlaceholder:@"smart公社注册邮箱"];
	
	if ([[NSUserDefaults standardUserDefaults] objectForKey:@"email"] != nil) {
		[userID setText:[[NSUserDefaults standardUserDefaults] objectForKey:@"email"]];
	}
	
	[sendView addSubview:userID];
	[userID release];
	
	password = [[UITextField alloc] initWithFrame:CGRectMake(20, 218, 210, 30)];
	[password setSecureTextEntry:YES];
	password.tag = 480;
	[password setDelegate:self];
	[password setBorderStyle:UITextBorderStyleRoundedRect];
	[password setPlaceholder:@"密码"];
	
	if ([[NSUserDefaults standardUserDefaults] objectForKey:@"password"] != nil) {
		[password setText:[[NSUserDefaults standardUserDefaults] objectForKey:@"password"]];
	}
	
	[sendView addSubview:password];
	[password release];
	
	UIButton *registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
	[registerButton setBackgroundImage:[UIImage imageNamed:@"register.png"] forState:UIControlStateNormal];
	[registerButton setFrame:CGRectMake(235, 180, 68, 58)];
	[registerButton addTarget:self action:@selector(registerAction) forControlEvents:UIControlEventTouchUpInside];
	[sendView addSubview:registerButton];
	
	
	UILabel *tips = [[UILabel alloc] initWithFrame:CGRectMake(20, 140, 300, 50)];
	[tips setText:@"不是smart公社会员？请先进行新用户注册！"];
	[tips setBackgroundColor:[UIColor clearColor]];
	[tips setFont:[UIFont systemFontOfSize:12]];
	[sendView addSubview:tips];
	[tips release];
	
	
	selectCity = [UIButton buttonWithType:UIButtonTypeCustom];
	selectCity.titleLabel.font = [UIFont boldSystemFontOfSize:13];
	[selectCity setBackgroundImage:[UIImage imageNamed:@"nearest.png"] forState:UIControlStateNormal];
	[selectCity addTarget:self action:@selector(selectCityAction) forControlEvents:UIControlEventTouchUpInside];
	
	if ([[NSUserDefaults standardUserDefaults] objectForKey:@"city"] != nil) {
		[selectCity setTitle:[[NSUserDefaults standardUserDefaults] objectForKey:@"city"] forState:UIControlStateNormal];
		self.submitCityString = [[NSUserDefaults standardUserDefaults] objectForKey:@"city"];

	}else {
		[selectCity setTitle:@"选择参与城市" forState:UIControlStateNormal];
        self.submitCityString = @"大连";
	}

	[selectCity setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
	[selectCity setFrame:CGRectMake(20, 258, 210, 35)];
	[sendView addSubview:selectCity];
	
	
	textView = [[UITextField alloc] initWithFrame:CGRectMake(20, 300, 280, 30)];
	textView.tag = 998;
	[textView setPlaceholder:@"我的都市新主张简述"];
	textView.delegate = self;
    [textView setBorderStyle:UITextBorderStyleRoundedRect];
	[sendView addSubview:textView];
	
	UIButton *submit = [UIButton buttonWithType:UIButtonTypeCustom];
	[submit addTarget:self action:@selector(submitToSmart) forControlEvents:UIControlEventTouchUpInside];
	submit.frame = CGRectMake(25, 350, 130, 30);
	[submit setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
	[submit setTitle:@"提 交" forState:UIControlStateNormal];
	[submit setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
	[sendView addSubview:submit];
	
	UIButton *reshoot = [UIButton buttonWithType:UIButtonTypeCustom];
	[reshoot addTarget:self action:@selector(shootPictureAndUpload) forControlEvents:UIControlEventTouchUpInside];
	reshoot.frame = CGRectMake(160, 350, 130, 30);
	[reshoot setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
	[reshoot setTitle:@"重 拍" forState:UIControlStateNormal];
	[reshoot setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
	[sendView addSubview:reshoot];
	
	//解决拍照后tabbar失灵
	SmartRoadShowAppDelegate *appDelegate = (SmartRoadShowAppDelegate *)[[UIApplication sharedApplication] delegate];
	MyTabBarController *myTabBarController = appDelegate.tabBarController;
	
	[myTabBarController selectControllerAtIndex:0];
	self = (RS3ViewController *)[((MyNavigationController*)[myTabBarController.viewControllers objectAtIndex:0]) topViewController];
	[self.segmentControl setSelectedSegmentIndex:1];
}

//提交到smart社区界面点击返回键盘

- (void)keyboardBack{
	[userID resignFirstResponder];
	[password resignFirstResponder];
	[textView resignFirstResponder];
	[sendView scrollRectToVisible:CGRectMake(0, 0, 320, 305) animated:YES];

}



//选择作品提交的城市
- (void)selectCityAction{
	
	[userID resignFirstResponder];
	[password resignFirstResponder];
	[textView resignFirstResponder];
	
	if (submitCityArray == nil) {
		submitCityArray = [[NSArray alloc] initWithObjects:@"大连",@"无锡",@"重庆",@"深圳",@"成都",@"南京",
						   @"杭州",@"昆明",@"温州",@"广州",@"上海",@"北京",nil];
	}
	
	UIView *pickerBG = [[UIView alloc] initWithFrame:CGRectMake(0, 40, 320, 305)];
	pickerBG.backgroundColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.95];
	[pickerBG setTag:789];
	pickerBG.layer.cornerRadius = 8;
	pickerBG.layer.masksToBounds = YES;
	[self.view addSubview:pickerBG];
	[pickerBG release];
	
	
	UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 320, 30)];
	titleLabel.font = [UIFont boldSystemFontOfSize:14];
	titleLabel.backgroundColor = [UIColor clearColor];
	titleLabel.textColor = [UIColor colorWithRed:0.97 green:0.80 blue:0.20 alpha:1];
	titleLabel.textAlignment = UITextAlignmentCenter;
	titleLabel.text = @"请选择您要参与的城市";
	[pickerBG addSubview:titleLabel];
	[titleLabel release];
	
	
	UIPickerView *cityPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 45, 320, 200)];
	[cityPickerView setBackgroundColor:[UIColor clearColor]];
	cityPickerView.layer.borderWidth = 10;
	cityPickerView.layer.borderColor = [[UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.95] CGColor];
	[cityPickerView setDelegate:self];
	[cityPickerView setDataSource:self];
	[cityPickerView setShowsSelectionIndicator:YES];
	[cityPickerView selectRow:[[[NSUserDefaults standardUserDefaults] objectForKey:@"cityIndex"] intValue] inComponent:0 animated:NO];
	[pickerBG addSubview:cityPickerView];
	[cityPickerView release];
	
	UIButton *selectedCity = [UIButton buttonWithType:UIButtonTypeCustom];
	[selectedCity addTarget:self action:@selector(citySelected) forControlEvents:UIControlEventTouchUpInside];
	[selectedCity setBackgroundImage:[UIImage imageNamed:@"nearest.png"] forState:UIControlStateNormal];
	[selectedCity setTitle:@"确定" forState:UIControlStateNormal];
	selectedCity.frame = CGRectMake(60, 265, 200, 35);
	[pickerBG addSubview:selectedCity];
}


//选择完城市
- (void)citySelected{
	for (UIView *subview in self.view.subviews){
		if (subview.tag == 789) {
			[subview removeFromSuperview];
		}
	}
	[selectCity setTitle:self.submitCityString forState:UIControlStateNormal];
	[[NSUserDefaults standardUserDefaults] setObject:self.submitCityString forKey:@"city"];
	[[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%d",lastTimeCityIndex] forKey:@"cityIndex"];
}

//用户注册界面
- (void)registerAction{
	
	if ([self showAlertWhileNoConnection]) {
		[sendView scrollRectToVisible:CGRectMake(0, 38, 320, 305) animated:YES];
		
		UIView *registerView = [[UIView alloc] initWithFrame:CGRectMake(5, 0, 310, 305)];
		[registerView setTag:456];
		registerView.backgroundColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.95];
		registerView.layer.cornerRadius = 8;
		registerView.layer.masksToBounds = YES;
		[sendView addSubview:registerView];
		[registerView release];
		
		UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 15, 310, 30)];
		titleLabel.backgroundColor = [UIColor clearColor];
		titleLabel.textColor = [UIColor colorWithRed:0.97 green:0.80 blue:0.20 alpha:1];
		titleLabel.textAlignment = UITextAlignmentCenter;
		titleLabel.text = @"创建smart公社帐号";
		[registerView addSubview:titleLabel];
		[titleLabel release];
		
		UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 110, 100, 30)];
		nameLabel.backgroundColor = [UIColor clearColor];
		nameLabel.textColor = [UIColor colorWithRed:0.97 green:0.80 blue:0.20 alpha:1];
		nameLabel.textAlignment = UITextAlignmentRight;
		nameLabel.text = @"昵称";
		[registerView addSubview:nameLabel];
		[nameLabel release];
		
		UILabel *password1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 150, 100, 30)];
		password1.backgroundColor = [UIColor clearColor];
		password1.textColor = [UIColor colorWithRed:0.97 green:0.80 blue:0.20 alpha:1];
		password1.textAlignment = UITextAlignmentRight;
		password1.text = @"密码";
		[registerView addSubview:password1];
		[password1 release];
		
		UILabel *password2 = [[UILabel alloc] initWithFrame:CGRectMake(10, 190, 100, 30)];
		password2.backgroundColor = [UIColor clearColor];
		password2.textColor = [UIColor colorWithRed:0.97 green:0.80 blue:0.20 alpha:1];
		password2.textAlignment = UITextAlignmentRight;
		password2.text = @"重复密码";
		[registerView addSubview:password2];
		[password2 release];
		
		UILabel *emailLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 70, 100, 30)];
		emailLabel.backgroundColor = [UIColor clearColor];
		emailLabel.textColor = [UIColor colorWithRed:0.97 green:0.80 blue:0.20 alpha:1];
		emailLabel.textAlignment = UITextAlignmentRight;
		emailLabel.text = @"邮件地址";
		[registerView addSubview:emailLabel];
		[emailLabel release];
		
		nameField = [[UITextField alloc] initWithFrame:CGRectMake(120,110, 160, 30)];
		[nameField setBorderStyle:UITextBorderStyleRoundedRect];
		[nameField setDelegate:self];
		nameField.tag = 410;
		[registerView addSubview:nameField];
		[nameField release];
		
		
		passwordField1 = [[UITextField alloc] initWithFrame:CGRectMake(120,150, 160, 30)];
		[passwordField1 setBorderStyle:UITextBorderStyleRoundedRect];
		[passwordField1 setSecureTextEntry:YES];
		passwordField1.tag = 411;
		[passwordField1 setDelegate:self];
		[registerView addSubview:passwordField1];
		[passwordField1 release];
		
		passwordField2 = [[UITextField alloc] initWithFrame:CGRectMake(120,190, 160, 30)];
		[passwordField2 setBorderStyle:UITextBorderStyleRoundedRect];
		[passwordField2 setSecureTextEntry:YES];
		passwordField2.tag = 412;
		[passwordField2 setDelegate:self];
		[registerView addSubview:passwordField2];
		[passwordField2 release];
		
		emailField = [[UITextField alloc] initWithFrame:CGRectMake(120,70, 160, 30)];
		[emailField setBorderStyle:UITextBorderStyleRoundedRect];
		[emailField setKeyboardType:UIKeyboardTypeEmailAddress];
		emailField.tag = 413;
		[emailField setDelegate:self];
		[registerView addSubview:emailField];
		[emailField release];
		
		[emailField becomeFirstResponder];
		
		UIButton *registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
		registerButton.frame = CGRectMake(40, 240, 110, 35);
		[registerButton addTarget:self action:@selector(submitRegisterAction) forControlEvents:UIControlEventTouchUpInside];
		[registerButton setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
		[registerButton setTitle:@"注册" forState:UIControlStateNormal];
		[registerButton.titleLabel setTextColor:[UIColor whiteColor]];
		[registerView addSubview:registerButton];
		
		UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
		backButton.frame = CGRectMake(175, 240, 110, 35);
		[backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
		[backButton setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
		[backButton setTitle:@"返回" forState:UIControlStateNormal];
		[backButton.titleLabel setTextColor:[UIColor whiteColor]];
		[registerView addSubview:backButton];
		
	}
	
	
}



//提交用户的注册信息
- (void)submitRegisterAction{
		
	NSString *nameStr = nameField.text;
	NSString *psd1 = passwordField1.text;
	NSString *psd2 = passwordField2.text;
	NSString *emailStr = emailField.text;
	
	
	NSString *meassage = nil;
	
	if (nameStr == nil || psd1 == nil || psd2 == nil || emailStr == nil) {
		meassage = @"请将信息填写完整！";
	}else if (![psd1 isEqualToString:psd2]) {
		meassage = @"您两次输入的密码不相符，请从新输入！";
	}else {
		//http://api.escdn.com/userRegister/?username=semon&password=md5-pwd&email=mymail@mail.com
		
		NSString *urlString = [NSString stringWithFormat:kRegister];
		
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
		[request setHTTPMethod:@"POST"];
		
		
		NSString *postStr = [NSString stringWithFormat:@"username=%@&password=%@&email=%@",nameStr,psd1,emailStr];
				
		NSLog(@"%@   %@   %@   %@",nameStr, psd1, psd2 ,emailStr);
		
		NSData *postData = [postStr dataUsingEncoding:NSUTF8StringEncoding];
		NSString *postDataLength = [NSString stringWithFormat:@"%d",[postData length]];
		[request setValue: postDataLength forHTTPHeaderField:@"Content-Length"];
		[request setHTTPBody:postData];
		
				
		NSError *error = nil;
		NSURLResponse *response = nil;
		NSData *urlData = [NSURLConnection 
						   sendSynchronousRequest:request 
						   returningResponse: &response 
						   error: &error];
		
		[request release];
		
		NSString *responseString = [[[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding] autorelease];
		NSLog(@"%s,%d,respone: %@",__FUNCTION__, __LINE__, responseString);
		
		meassage = @"注册成功！";
		
		
	}

	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
													  message:meassage
													 delegate:self
											cancelButtonTitle:@"确定"
											otherButtonTitles:nil];
	
	[alert setTag:555];
	[alert show];
	[alert release];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
	
	if (alertView.tag == 975) {
		for (UIView *subview in shareToSina.subviews){
			if (subview.tag == 596) {
				[subview removeFromSuperview];
			}
		}
		
		for (UIView *subview in self.view.subviews){
			if (subview.tag == 300 || subview.tag == 302) {
				[subview removeFromSuperview];
			}
		}
		[self.view addSubview:secondView];
		
	}
	
	else if ([alertView.message isEqualToString:@"注册成功！"]) {
		for (UIView *subview in sendView.subviews){
			if (subview.tag == 456) {
				[subview removeFromSuperview];
			}
		}
		[sendView scrollRectToVisible:CGRectMake(0, 0, 320, 305) animated:YES];
	}
}

- (void)backAction{
	for (UIView *subview in sendView.subviews){
		if (subview.tag == 456) {
			[subview removeFromSuperview];
		}
	}
	[sendView scrollRectToVisible:CGRectMake(0, 0, 320, 305) animated:YES];
}


//将作品提交到smart社区
- (void)submitToSmart{
	
	Reachability *internetReach = [Reachability reachabilityForInternetConnection];
	[internetReach startNotifier];	
	NetworkStatus netStatus = [internetReach currentReachabilityStatus];
	if (netStatus) {
		if (YES) {
			userIDStr = userID.text;
			passWordStr = password.text;
			description = textView.text;

			if (userIDStr == nil || passWordStr == nil || description == nil || self.submitCityString == nil) {
				UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
																message:@"请将信息填写完整再提交！"
															   delegate:nil
													  cancelButtonTitle:@"确定" otherButtonTitles:nil];
				[alert show];
				[alert release];
			}else {
				
				//post imageData
				OpenWaitView *waitUpload = [[OpenWaitView alloc] initWithView:self.view];
				[waitUpload setMode:MBProgressHUDModeIndeterminate];
				[waitUpload setLabelText:@"正在提交..."];
				[waitUpload showWhileExecuting:@selector(ExeUploadToSmart) onTarget:self
									withObject:nil animated:YES];
				[self.view addSubview:waitUpload];
				[waitUpload release];
			}
		}
		else {
			NSLog(@"上传失败！");
		}
	}
	else {
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"本功能需要网络支持，请检查网络连接！"
													   delegate:nil cancelButtonTitle:@"确定"
											  otherButtonTitles:nil];
		[alert show];
		[alert release];
	}
		
}

- (void)ExeUploadToSmart{
	
    //存储用户信息
	[[NSUserDefaults standardUserDefaults] setObject:userIDStr forKey:@"email"];
	[[NSUserDefaults standardUserDefaults] setObject:passWordStr forKey:@"password"];
	[[NSUserDefaults standardUserDefaults] setObject:self.submitCityString forKey:@"city"];
	
	NSData *imageData = UIImageJPEGRepresentation(self.shareImage, 1); //image转码
	
	
	NSString *urlString = [NSString stringWithFormat:kUploadImage];
	
	NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
	[request setHTTPMethod:@"POST"];
	
	NSString *stringBoundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"];
	NSString * contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", stringBoundary];
	[request addValue:contentType forHTTPHeaderField:@"Content-Type"]; 
	NSString *imageFileName = [NSString stringWithFormat:@"photo.jpg"];
	
	NSMutableData *postBody = [NSMutableData data];
	
	[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
	[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image\"; filename=\"%@\"\r\n",imageFileName] dataUsingEncoding:NSUTF8StringEncoding]];
	[postBody appendData:[@"Content-Type: image/jpg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
	[postBody appendData:imageData];
	[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
	
	
	[postBody appendData:[[NSString stringWithFormat:@"\r\n\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
	[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"username\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
	[postBody appendData:[[NSString stringWithString:userIDStr] dataUsingEncoding:NSUTF8StringEncoding]]; 
	
	[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
	[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"password\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
	[postBody appendData:[[NSString stringWithString:passWordStr] dataUsingEncoding:NSUTF8StringEncoding]];  
	
	[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
	[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"title\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
	[postBody appendData:[[NSString stringWithString:description] dataUsingEncoding:NSUTF8StringEncoding]]; 				
	
	[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
	[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"city\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
	[postBody appendData:[[NSString stringWithString:self.submitCityString] dataUsingEncoding:NSUTF8StringEncoding]];
	
	[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
	[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"timestamp\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
	[postBody appendData:[[NSString stringWithString:[NSString stringWithFormat:@"%@",[NSDate date]]] dataUsingEncoding:NSUTF8StringEncoding]]; 
	
	
	NSString *postDataLength = [NSString stringWithFormat:@"%d",[postBody length]];
	[request setValue: postDataLength forHTTPHeaderField:@"Content-Length"];
	
	[request setHTTPBody:postBody];
	
	NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
	NSString *responseString = [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] autorelease];
	
	NSLog(@"%s,%d,respone: %@",__FUNCTION__, __LINE__, responseString);
	[request release];
	
	
	
	for (UIView *subview in self.view.subviews){
		if (subview.tag == 301) {
			[subview removeFromSuperview];
		}
	}
	
	
	shareToSina = [[UIView alloc] initWithFrame:CGRectMake(5, 40, 310, 303)];
	[shareToSina setTag:301];
	shareToSina.layer.contents = (id)[UIImage imageNamed:@"tableViewBackground.png"].CGImage;
	shareToSina.layer.cornerRadius = 8;
	shareToSina.layer.masksToBounds = YES;
	[shareToSina setBackgroundColor:[UIColor clearColor]];
	[self.view addSubview:shareToSina];
	[shareToSina setTag:301];
	[shareToSina release];
	
	UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 290, 180)];
	[imageView setContentMode:UIViewContentModeScaleAspectFit];
	[imageView setImage:self.shareImage];
	[shareToSina addSubview:imageView];
	[imageView release];
	
	UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 195, 280, 70)];
	[label setNumberOfLines:0];
	[label setText:@"感谢参与，您的作品已经成功提交到smart城市主张活动！\n想朋友分享你的城市主张吗？快转发微博吧！"];
	[label setBackgroundColor:[UIColor clearColor]];
	[label setFont:[UIFont systemFontOfSize:13]];
	[shareToSina addSubview:label];
	[label release];
	
	UIButton *submit = [UIButton buttonWithType:UIButtonTypeCustom];
	submit.titleLabel.font = [UIFont systemFontOfSize:13];
	[submit addTarget:self action:@selector(submitToSina) forControlEvents:UIControlEventTouchUpInside];
	submit.frame = CGRectMake(25, 270, 130, 30);
	[submit setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
	[submit setTitle:@"转发新浪微博" forState:UIControlStateNormal];
	[submit setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
	[shareToSina addSubview:submit];
	
	UIButton *reshoot = [UIButton buttonWithType:UIButtonTypeCustom];
	reshoot.titleLabel.font = [UIFont systemFontOfSize:13];
	[reshoot addTarget:self action:@selector(shootPictureAndUpload) forControlEvents:UIControlEventTouchUpInside];
	reshoot.frame = CGRectMake(160, 270, 130, 30);
	[reshoot setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
	[reshoot setTitle:@"再拍一张" forState:UIControlStateNormal];
	[reshoot setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
	[shareToSina addSubview:reshoot];
	
	
}


//登录新浪微波界面的返回键盘
- (void)SinakeyboardBack{
	[sinaNameField resignFirstResponder];
	[sinaPassword resignFirstResponder];
	[sinaTextView resignFirstResponder];
}


//提交到新浪微博
- (void)submitToSina{
	
	UIView *loginSina = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 310, 303)];
	loginSina.tag = 596;
	loginSina.layer.cornerRadius = 8;
	loginSina.layer.masksToBounds = YES;
	loginSina.backgroundColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.95];
      
	
	UIControl *control = [[UIControl alloc] initWithFrame:CGRectMake(0, 0, 310, 303)];
	[control addTarget:self action:@selector(SinakeyboardBack) forControlEvents:UIControlEventTouchUpInside];
	[loginSina addSubview:control];
	[control release];
	
	[shareToSina addSubview:loginSina];
	[loginSina release];
	
	UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 15, 310, 30)];
	titleLabel.backgroundColor = [UIColor clearColor];
	titleLabel.textColor = [UIColor colorWithRed:0.97 green:0.80 blue:0.20 alpha:1];
	titleLabel.textAlignment = UITextAlignmentCenter;
	titleLabel.text = @"请先登录新浪微博";
	[loginSina addSubview:titleLabel];
	[titleLabel release];
	
	UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 70, 80, 30)];
	nameLabel.backgroundColor = [UIColor clearColor];
	nameLabel.textColor = [UIColor colorWithRed:0.97 green:0.80 blue:0.20 alpha:1];
	nameLabel.text = @"帐号";
	[loginSina addSubview:nameLabel];
	[nameLabel release];
	
	sinaNameField = [[UITextField alloc] initWithFrame:CGRectMake(80,70, 200, 30)];
	sinaNameField.tag = 5566;
	
	if ([[NSUserDefaults standardUserDefaults] objectForKey:@"sinaID"] != nil) {
		[sinaNameField setText:[[NSUserDefaults standardUserDefaults] objectForKey:@"sinaID"]];
	}

	[sinaNameField setBorderStyle:UITextBorderStyleRoundedRect];
	[sinaNameField becomeFirstResponder];
	
	[sinaNameField setDelegate:self];
	[loginSina addSubview:sinaNameField];
	[sinaNameField release];
	
	
	UILabel *password1 = [[UILabel alloc] initWithFrame:CGRectMake(30, 110, 80, 30)];
	password1.backgroundColor = [UIColor clearColor];
	
	password1.tag = 7788;
	
	password1.textColor = [UIColor colorWithRed:0.97 green:0.80 blue:0.20 alpha:1];
	password1.text = @"密码";
	[loginSina addSubview:password1];
	[password1 release];
	
	sinaPassword = [[UITextField alloc] initWithFrame:CGRectMake(80,110, 200, 30)];
	[sinaPassword setBorderStyle:UITextBorderStyleRoundedRect];
	[sinaPassword setSecureTextEntry:YES];
	
	if ([[NSUserDefaults standardUserDefaults] objectForKey:@"sinaPassword"]) {
		[sinaPassword setText:@"sinaPassword"];
	}
	
	[sinaPassword setDelegate:self];
	[loginSina addSubview:sinaPassword];
	[sinaPassword release];
	
	
	UILabel *comment = [[UILabel alloc] initWithFrame:CGRectMake(30, 150, 80, 30)];
	[comment setText:@"评论"];
	comment.backgroundColor = [UIColor clearColor];
	comment.textColor = [UIColor colorWithRed:0.97 green:0.80 blue:0.20 alpha:1];
	[loginSina addSubview:comment];
	[comment release];
	
	
	sinaTextView = [[UITextView alloc] initWithFrame:CGRectMake(80, 150, 200, 80)];
	sinaTextView.layer.cornerRadius = 5;
	sinaTextView.layer.masksToBounds = YES;
	
	[sinaTextView setText:textView.text];
	
	[loginSina addSubview:sinaTextView];
	[sinaTextView release];
	
	
	UIButton *done = [UIButton buttonWithType:UIButtonTypeCustom];
	[done addTarget:self action:@selector(uploadSina) forControlEvents:UIControlEventTouchUpInside];
	[done setFrame:CGRectMake(50, 250, 100, 30)];
	[done setTitle:@"转发" forState:UIControlStateNormal];
	[done setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
	[loginSina addSubview:done];
	
	UIButton *cancel = [UIButton buttonWithType:UIButtonTypeCustom];
	[cancel addTarget:self action:@selector(cancelUploadToSina) forControlEvents:UIControlEventTouchUpInside];
	[cancel setFrame:CGRectMake(160, 250, 100, 30)];
	[cancel setTitle:@"取消" forState:UIControlStateNormal];
	[cancel setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
	[loginSina addSubview:cancel];
	
	
}

- (void)cancelUploadToSina{
	for (UIView *subview in shareToSina.subviews){
		if (subview.tag == 596) {
			[subview removeFromSuperview];
		}
	}
	
}

- (void)uploadSina{
	NSString *sinaID = sinaNameField.text;
	NSString *sinaPsd = sinaPassword.text;
	
	[[NSUserDefaults standardUserDefaults] setObject:sinaID forKey:@"sinaID"];
	[[NSUserDefaults standardUserDefaults] setObject:sinaPsd forKey:@"sinaPassword"];
	
	NSString *authString = [NSString stringWithFormat:@"%@:%@",sinaID,sinaPsd];
	NSData *authData = [authString dataUsingEncoding:NSUTF8StringEncoding];
	NSString *authValue = [NSString stringWithFormat:@"Basic %@",[authData base64EncodingWithLineLength:80]];
	
	NSURL *url = [NSURL URLWithString:kSinaVerify];
	NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
	[request setHTTPMethod:@"GET"];
	[request setValue:authValue forHTTPHeaderField:@"Authorization"];
	
	NSURLResponse *response;
	NSError *error;
	
	[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
	
	NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
	
	int statusCode = [httpResponse statusCode];
	
	NSLog(@"status code = %d",statusCode);
	
	/////////////////////////////////////////////////发布////////////////////////////////////////////////
	if (statusCode == 200) {
		NSLog(@"认证成功！");
		NSString *boundary = @"0xKhTmLbOuNdArYckkk";  
		NSString *filename = @"都市新主张.jpg";			
		NSData *imageData = UIImageJPEGRepresentation(shareImage, 1);
		
		
		NSString *bodyPrefixString   = [NSString stringWithFormat:@"--%@\r\n", boundary];
		NSString *bodySuffixString   = [NSString stringWithFormat:@"\r\n--%@--\r\n", boundary];
		NSString *contentDisposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"pic\"; filename=\"%@\"\r\n", filename];
		NSString *contentImageType   = [NSString stringWithFormat:@"Content-Type: image/%@\r\n", [filename pathExtension]];
		NSString *contentTransfer    = @"Content-Transfer-Encoding: binary\r\n\r\n";
		
		NSString *bodyUpdateField = [NSString stringWithFormat:@"Content-Disposition: form-data;name=\"status\"\r\n\r\n#smart都市新主张#%@ http://urbanbeatsroadshow.the-smart-class.cn/photo.html 定格你的smart行为大话生活新主张，赢取smart好礼！\r\n",sinaTextView.text];// 将test换成发送的文本
		
		NSMutableString *sendString = [[NSMutableString alloc] init];
		[sendString appendString:bodyPrefixString];
		[sendString appendString:bodyUpdateField];
		[sendString appendString:bodyPrefixString];
		[sendString appendString:contentDisposition];
		[sendString appendString:contentImageType];
		[sendString appendString:contentTransfer];
		[sendString appendString:bodySuffixString];
		
		//temp 
		NSMutableData *postBody = [NSMutableData data];
		
		[postBody appendData:[bodyPrefixString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO]];
		[postBody appendData:[bodyUpdateField dataUsingEncoding:NSUTF8StringEncoding ]];
		[postBody appendData:[bodyPrefixString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO]];
		[postBody appendData:[contentDisposition dataUsingEncoding:NSUTF8StringEncoding ]];
		[postBody appendData:[contentImageType dataUsingEncoding:NSUTF8StringEncoding ]];
		[postBody appendData:[contentTransfer dataUsingEncoding:NSUTF8StringEncoding]];
		[postBody appendData:imageData];
		
		[postBody appendData:[bodySuffixString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO]];
		
		
		NSString *baseURL = [NSString stringWithFormat:@"http://api.t.sina.com.cn/statuses/upload.xml?source=4143216683"];//&status=这个不能要
		NSURL *url = [NSURL URLWithString:baseURL];
		//2992135204
		NSMutableURLRequest *mainRequest = [[NSMutableURLRequest alloc] initWithURL:url
																		cachePolicy:NSURLRequestUseProtocolCachePolicy
																	timeoutInterval:30.0f];
		
		[mainRequest setHTTPMethod:@"POST"];
		[mainRequest setValue:authValue forHTTPHeaderField:@"Authorization"];
		NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary, nil];
		[mainRequest setValue:contentType forHTTPHeaderField:@"Content-Type"];
		[mainRequest setHTTPBody:postBody];
		
		NSURLResponse *shareResponse;
		NSError *error;
		
		NSData *responseData = [NSURLConnection sendSynchronousRequest:mainRequest returningResponse:&shareResponse error:&error];
		NSString *responseString = [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] autorelease];
		
		[mainRequest release];
		
		
		NSLog(@"%s,%d,respone: %@",__FUNCTION__, __LINE__, responseString);
		if (responseString != nil && [responseString isEqualToString:@""]) {
			//分享成功！
			NSLog(@"分享成功！");
			NSArray *responsePartsArray = [responseString componentsSeparatedByString:@"</id>"];
			NSString *responseStringPart1 = [responsePartsArray objectAtIndex:0];
			NSString *responseStringPart2 = [responsePartsArray objectAtIndex:1];
			NSArray *IDArray = [responseStringPart1 componentsSeparatedByString:@"<id>"];
			NSArray *userIDArray = [responseStringPart2 componentsSeparatedByString:@"<id>"];
			NSString *_userID = [userIDArray objectAtIndex:1];
			NSString *ID = [IDArray objectAtIndex:1];
			NSURL *messageURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://api.t.sina.com.cn/%@/statuses/%@",_userID,ID]];
			NSMutableURLRequest *getMessageRequest = [[NSMutableURLRequest alloc] initWithURL:messageURL];
			[getMessageRequest setHTTPMethod:@"GET"];
			[getMessageRequest setValue:authValue forHTTPHeaderField:@"Authorization"];
			
			[getMessageRequest release];
			//[responseString release];
		}
		
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"转发新浪微博成功！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
		[alert setTag:975];
		[alert show];
		[alert release];
        [sendString release];
	}
	else {
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您的用户名或密码错误，请从新输入！" delegate:nil
											  cancelButtonTitle:@"确定" otherButtonTitles:nil];
		[alert show];
		[alert release];
	}
	
    [request release];
   // [sendStrin];
}


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
//		if (textField.tag == 998) {
//			[sendView scrollRectToVisible:CGRectMake(0, 200, 320, 305) animated:YES];
//		}
//		else if (textField.tag != 5566 && textField.tag != 7788){
//			[sendView scrollRectToVisible:CGRectMake(0, 150, 320, 305) animated:YES];
//			
//	}
	switch (textField.tag) {
		case 480:
		case 998:
			[sendView scrollRectToVisible:CGRectMake(0, 150, 320, 305) animated:YES];
			break;
		case 410:
		case 411:
		case 412:
		case 413:
			[sendView scrollRectToVisible:CGRectMake(0, 50, 320, 305) animated:YES];
			break;

		default:
			break;
	}
	
	return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
	switch (textField.tag) {
		case 998:
			[sendView scrollRectToVisible:CGRectMake(0, 80, 320, 305) animated:YES];
			break;
		case 480:
		case 412:
			[sendView scrollRectToVisible:CGRectMake(0, 0, 320, 305) animated:YES];
			break;
		default:
			break;
	}
 //   [sendView scrollRectToVisible:CGRectMake(0, 0, 320, 305) animated:YES];
	[textField resignFirstResponder];
	return YES;
}


#pragma mark -
#pragma mark Camera View Delegate Methods

- (void)imagePickerController:(UIImagePickerController *)picker 
didFinishPickingMediaWithInfo:(NSDictionary *)info {
    
    [picker dismissModalViewControllerAnimated:YES];
    UIImage *image = [[info objectForKey:UIImagePickerControllerEditedImage] retain];
    [self performSelector:@selector(didFinishShoot:) 
               withObject:image 
               afterDelay:0.5];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
	NSLog(@"cancel");
    [picker dismissModalViewControllerAnimated:YES];
	
	//解决拍照后tabbar失灵
	SmartRoadShowAppDelegate *appDelegate = (SmartRoadShowAppDelegate *)[[UIApplication sharedApplication] delegate];
	MyTabBarController *myTabBarController = appDelegate.tabBarController;
	
	[myTabBarController selectControllerAtIndex:0];
	self = (RS3ViewController *)[((MyNavigationController*)[myTabBarController.viewControllers objectAtIndex:0]) topViewController];
	[self.segmentControl setSelectedSegmentIndex:1];
}



- (void)ScrollViewScrollToCityIndex:(NSInteger) index{
	[scrollView setContentOffset:CGPointMake(index * 270, 0) animated:NO];
}


#pragma mark UIScrollView Delegate
- (void)scrollViewDidScroll:(UIScrollView *)_scrollView{
	CGFloat pageWidth = 270;
    int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
	currentScrollPage = page;

}

//- (void)scrollViewDidEndDragging:(UIScrollView *)_scrollView willDecelerate:(BOOL)decelerate{
//	[scrollView setContentOffset:CGPointMake(currentScrollPage * 270, 0) animated:YES];
//}
//
//- (void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView{
//	[scrollView setContentOffset:CGPointMake(currentScrollPage * 270, 0) animated:YES];
//	
//}
//
//- (void)scrollViewWillBeginDecelerating:(UIScrollView *)_scrollView{
//	[scrollView setContentOffset:CGPointMake(currentScrollPage * 270, 0) animated:YES];
//}





#pragma mark UITableView Delegate & DataSource
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section{
	return [opinionsArray count];
}

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	static NSString *cutoutIdentifier = @"CutoutCell";
	NSInteger row = indexPath.row;

	MyTableViewCell *cell = (MyTableViewCell *)[aTableView dequeueReusableCellWithIdentifier:cutoutIdentifier];
	if (cell == nil) {
		cell = [[[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cutoutIdentifier] autorelease];
		cell.accessoryType = UITableViewCellAccessoryNone;
		[cell setSelectionStyle:UITableViewCellSelectionStyleNone];	
	}
	
	
	
	
	Opinion *op = [opinionsArray objectAtIndex:row];
	
//	MyTableViewCell *cell = nil;
//    if (cell == nil) {
//		cell = [[[MyTableViewCell alloc] init] autorelease];
//		cell.accessoryType = UITableViewCellAccessoryNone;
//		[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
//    }
	cell.delegate = self;
	[cell setTag:row];
	[cell setImageWithURL:[op thumb]];
	[cell setTitleLabelText:[op title]];
	[cell setDetailLabelText:[NSString stringWithFormat:@"By %@\n\n%@" ,[op user],[op addDate]]];
	return cell;
}	

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
	DetailWebViewController *detailVC = [[DetailWebViewController alloc] init];
	
	NSString *urlStr = [NSString stringWithFormat:@"%@?itemid=%d",kCityShootInfo,[[opinionsArray objectAtIndex:indexPath.row
																																  ] itemId]];
	
	[detailVC.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]]];
	[self.navigationController pushViewController:detailVC animated:YES];
	[detailVC release];
	
}

- (void)seeDetailInfo:(NSInteger)index{
	DetailWebViewController *detailVC = [[DetailWebViewController alloc] init];
	
	NSString *urlStr = [NSString stringWithFormat:@"%@?itemid=%d",kCityShootInfo,[[opinionsArray objectAtIndex:index
																																		  ] itemId]];
	
	[detailVC.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]]];
	[self.navigationController pushViewController:detailVC animated:YES];
	[detailVC release];
	
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{	
	return 130;
}


#pragma mark UIPickerView Delegate & DataSource

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
	return 200;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [submitCityArray count];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
	return 1;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
	self.submitCityString = [submitCityArray objectAtIndex:row];
	lastTimeCityIndex = row;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row 
		  forComponent:(NSInteger)component reusingView:(UIView *)view{
	
	UILabel *titleLabel = [[UILabel alloc] init];
	[titleLabel setFont:[UIFont systemFontOfSize:22]];
	[titleLabel setBackgroundColor:[UIColor clearColor]];
	
	titleLabel.frame = CGRectMake(70, 0, 60, 40);
	[titleLabel setText:[submitCityArray objectAtIndex:row]];	
	return [titleLabel autorelease];
}




- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return YES;
}

-(void) pushMoreInfoViewController{
	
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}


- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
	firstView = nil;
	secondView = nil;
	thirdWebView = nil;
	scrollView = nil;
}


- (void)dealloc {
	[firstView release];
	[secondView release];
	[thirdWebView release];
	[scrollView release];
	
	[stationArray release];
	[citysArr release];
    [super dealloc];
}


@end
